<?php 
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id

	+ Module  		: m_payment Model
	+ Description	: For record model process back-end
	+ Filename 		: c_m_payment.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 11:40:14

*/

class M_m_payment extends CI_Model{

		//constructor
		function __construct() {
			parent::__construct();
		}

		//function for get list record
		function m_payment_list($filter,$start,$end){
			$sql = "SELECT payment_id,
						   payment_name,
						   payment_active,
						   payment_created_date,
						   payment_created_by,
						   payment_updated_date,
						   payment_updated_by,
						   payment_updated_count					FROM   m_payment";

			// For simple search
			if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(payment_name) LIKE '%".addslashes(strtolower($filter))."%')";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." LIMIT ".$start.", ".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function m_payment_create(
				 $payment_name,
				 $payment_active,
				 $payment_created_date,
				 $payment_created_by,
				 $payment_updated_date,
				 $payment_updated_by,
				 $payment_updated_count){
			$data = array(
						 "payment_name"=>$payment_name,
						 "payment_active"=>$payment_active,
						 "payment_created_date"=>$payment_created_date,
						 "payment_created_by"=>$payment_created_by,
						 "payment_updated_date"=>$payment_updated_date,
						 "payment_updated_by"=>$payment_updated_by,
						 "payment_updated_count"=>$payment_updated_count					);
			$this->db->insert('m_payment', $data);
			if($this->db->affected_rows())
				return '1';
			else
				return '0';
		}

		//function for update record
		function m_payment_update($payment_id,
				 $payment_name,
				 $payment_active,
				 $payment_created_date,
				 $payment_created_by,
				 $payment_updated_date,
				 $payment_updated_by,
				 $payment_updated_count){
			$data = array(
						"payment_name"=>$payment_name,
						"payment_active"=>$payment_active,
						"payment_created_date"=>$payment_created_date,
						"payment_created_by"=>$payment_created_by,
						"payment_updated_date"=>$payment_updated_date,
						"payment_updated_by"=>$payment_updated_by,
						"payment_updated_count"=>$payment_updated_count					);

			$this->db->where('payment_id', $payment_id);
			$this->db->update('m_payment', $data);
			return '1';
		}

		//fcuntion for delete record
		function m_payment_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the m_payments at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM m_payment WHERE payment_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM m_payment WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "payment_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function m_payment_search($payment_id,
				 $payment_name,
				 $payment_active,
				 $payment_created_date,
				 $payment_created_by,
				 $payment_updated_date,
				 $payment_updated_by,
				 $payment_updated_count,$start,$end){
			//full query
			$sql = "SELECT payment_id,
						   payment_name,
						   payment_active,
						   payment_created_date,
						   payment_created_by,
						   payment_updated_date,
						   payment_updated_by,
						   payment_updated_count
					FROM   m_payment";

			/* if($payment_id!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_id) LIKE '%".addslashes(strtolower($payment_id))."%'";
			}; */
			if($payment_name!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_name) LIKE '%".addslashes(strtolower($payment_name))."%'";
			};
			if($payment_active!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_active) LIKE '%".addslashes(strtolower($payment_active))."%'";
			};
			/* if($payment_created_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_created_date) LIKE '%".addslashes(strtolower($payment_created_date))."%'";
			};
			if($payment_created_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_created_by) LIKE '%".addslashes(strtolower($payment_created_by))."%'";
			};
			if($payment_updated_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_updated_date) LIKE '%".addslashes(strtolower($payment_updated_date))."%'";
			};
			if($payment_updated_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_updated_by) LIKE '%".addslashes(strtolower($payment_updated_by))."%'";
			};
			if($payment_updated_count!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(payment_updated_count) LIKE '%".addslashes(strtolower($payment_updated_count))."%'";
			}; */

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function m_payment_print($payment_id,
				 $payment_name,
				 $payment_active,
				 $payment_created_date,
				 $payment_created_by,
				 $payment_updated_date,
				 $payment_updated_by,
				 $payment_updated_count,$option,$filter){
			//full query
			$sql = "SELECT payment_id,
						   payment_name,
						   payment_active,
						   payment_created_date,
						   payment_created_by,
						   payment_updated_date,
						   payment_updated_by,
						   payment_updated_count
				    FROM   m_payment";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(payment_name) LIKE '%".addslashes(strtolower($filter))."%')";
			} else if($option=='SEARCH'){
				/* if($payment_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_id) LIKE '%".addslashes(strtolower($payment_id))."%'";
				}; */
				if($payment_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_name) LIKE '%".addslashes(strtolower($payment_name))."%'";
				};
				if($payment_active!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_active) LIKE '%".addslashes(strtolower($payment_active))."%'";
				};
				/* if($payment_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_created_date) LIKE '%".addslashes(strtolower($payment_created_date))."%'";
				};
				if($payment_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_created_by) LIKE '%".addslashes(strtolower($payment_created_by))."%'";
				};
				if($payment_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_updated_date) LIKE '%".addslashes(strtolower($payment_updated_date))."%'";
				};
				if($payment_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_updated_by) LIKE '%".addslashes(strtolower($payment_updated_by))."%'";
				};
				if($payment_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_updated_count) LIKE '%".addslashes(strtolower($payment_updated_count))."%'";
				}; */
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function m_payment_export_excel($payment_id ,$payment_name ,$payment_active ,$payment_created_date ,$payment_created_by ,$payment_updated_date ,$payment_updated_by ,$payment_updated_count ,$option,$filter){
			//full query
			$sql = "SELECT payment_id,
						   payment_name,
						   payment_active,
						   payment_created_date,
						   payment_created_by,
						   payment_updated_date,
						   payment_updated_by,
						   payment_updated_count				    FROM   m_payment";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= "( lower(payment_name) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				/* if($payment_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_id) LIKE '%".addslashes(strtolower($payment_id))."%'";
				}; */
				if($payment_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_name) LIKE '%".addslashes(strtolower($payment_name))."%'";
				};
				if($payment_active!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_active) LIKE '%".addslashes(strtolower($payment_active))."%'";
				};
				/* if($payment_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_created_date) LIKE '%".addslashes(strtolower($payment_created_date))."%'";
				};
				if($payment_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_created_by) LIKE '%".addslashes(strtolower($payment_created_by))."%'";
				};
				if($payment_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_updated_date) LIKE '%".addslashes(strtolower($payment_updated_date))."%'";
				};
				if($payment_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_updated_by) LIKE '%".addslashes(strtolower($payment_updated_by))."%'";
				};
				if($payment_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(payment_updated_count) LIKE '%".addslashes(strtolower($payment_updated_count))."%'";
				}; */
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>